package com.apptonelabs.android.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apptonelabs.android.R;
import com.apptonelabs.android.controller.PNREntryActivity;
import com.apptonelabs.android.model.PNREntryList;
import com.apptonelabs.android.model.PNRStatus;
import com.apptonelabs.android.model.PNRType;
import com.apptonelabs.android.model.PassengerDetails;
import com.apptonelabs.android.util.TimeUtil;

public class PNRArrayAdapter extends ArrayAdapter<PNRStatus> {
	private final Context context;
	private final List<PNRStatus> values;
	private final PNRType type;

	public PNRArrayAdapter(Context context, List<PNRStatus> values,PNRType type) {
		super(context, R.layout.pnrlist, values);
		this.context = context;
		this.values = values;
		this.type = type;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = inflater.inflate(R.layout.pnrlist, parent, false);
		TextView textView = (TextView) rowView.findViewById(R.id.pnrLableTV);
		TextView textView2 = (TextView) rowView.findViewById(R.id.pnrLableTV2);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
		
		// Position == 0 should be the Title of the Activity
		if(position == 0) {
			// Font settings
			textView.setTextSize(18F);
			textView2.setTextSize(16F);

			imageView.setImageResource(R.drawable.ic_launcher);
			String text = "<font color=#395563><b>Indian Railway PNR Status</b></font>";
			textView.setText(Html.fromHtml(text));
			if(PNRType.FUTURE == type) {
				textView2.setText("You have " + (this.values.size()-1) + " up coming Journeys.");
			} else if(PNRType.PAST == type) {
				textView2.setText("You travelled " + (this.values.size()-1) + " times!!!");
			} else {
				textView2.setText("Today, you have " + (this.values.size()-1) + " Journeys.");
			}
			return rowView;
		}

		// Font settings
		textView.setTextSize(16F);
		textView2.setTextSize(16F);
		
		// Text
		String text = 
				"<b>"
				+ "<font color=#008000>"
				+ values.get(position).getTrainDetails().getBoardingDate()
				+ "</font>"
				+ " <font color=#ff0000>"
				+ values.get(position).getPnr()
				+ "</font>"
				+ " <font color=#0000ff>"
				+ values.get(position).getTrainDetails().getReservedType()
				+ "</font>"
				+ "</b>"
				;
		
		textView.setText(Html.fromHtml(text));
		
		
		textView2.setText(
				"Train#: " + values.get(position).getTrainDetails().getNumber().substring(1,
						values.get(position).getTrainDetails().getNumber().length())
				+ ", "
				+ values.get(position).getTrainDetails().getFrom().trim()
				+ " -> "
				+ values.get(position).getTrainDetails().getTo().trim()
				+ ", @ "
				+ values.get(position).getTrainDetails().getBoardingPoint()
						.trim());

		// Change icon based on name
		String br = values.get(position).getPassengerDetails().get(0)
				.getCurrentStatus();

		String cs = values.get(position).getTrainDetails().getChatStatus();

		if (br.startsWith("CNF") || br.startsWith("RAC")
				|| br.startsWith("Can")) {
			imageView.setImageResource(R.drawable.cnf);
		} else if (br.startsWith("W")
				&& cs.equalsIgnoreCase("CHART NOT PREPARED")) {
			imageView.setImageResource(R.drawable.wlo);
		} else if (br.startsWith("W")
				&& !cs.equalsIgnoreCase("CHART NOT PREPARED")) {
			imageView.setImageResource(R.drawable.wlr);
		} else {
			imageView.setImageResource(R.drawable.cnf);
		}

		rowView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPNRDetails(position);
			}
		});
		
		return rowView;
	}
	
	private void showPNRDetails(final int position) {
		List<String> list = new ArrayList<String>();
		PNREntryList obj = new PNREntryList();
		try {
			Intent to = null;
			Bundle bundle = new Bundle();
			list.add("Title");
			list.add("<b>PNR Number		:</b> " + values.get(position).getPnr());
			list.add("<b>Journey Date		:</b> " + TimeUtil.date2long(values.get(position).getTrainDetails().getBoardingDate()));
			list.add("<b>Train number	:</b> " + values.get(position).getTrainDetails().getNumber().substring(1,
					values.get(position).getTrainDetails().getNumber().length()));
			list.add("<b>Current status	:</b> " + values.get(position).getPassengerDetails().get(0).getCurrentStatus());
			list.add("<b>Train from		:</b> " + values.get(position).getTrainDetails().getFrom());
			list.add("<b>Boradring point	:</b> " + values.get(position).getTrainDetails().getBoardingPoint());
			list.add("<b>Train to			:</b> " + values.get(position).getTrainDetails().getTo());
			list.add("<b>Reservation up to:</b> " + values.get(position).getTrainDetails().getReservedUpto());
			list.add("<b>Passengers		:</b> Booked, Current status");
			for(PassengerDetails pd : values.get(position).getPassengerDetails()) {
				list.add(pd.getName() + " " + pd.getBookingStatus() + ", " + pd.getCurrentStatus());
			}

			obj.setList(list);
			
			bundle.putSerializable("pnrlist", obj);
			bundle.putSerializable("pnrlisttype", type);

			to = new Intent(this.getContext(), PNREntryActivity.class);
			to.putExtras(bundle);
			this.getContext().startActivity(to);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

}